﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using WindowsPhoneApplication1.Connectors;
using Microsoft.Phone.Shell;
using System.Windows.Threading;

namespace WindowsPhoneApplication1
{
    public partial class StartNoLoginPanorama : PhoneApplicationPage
    {
        ApplicationBarIconButton save;
        bool isInit = false;
        public StartNoLoginPanorama()
        {
            InitializeComponent();
            isInit = true;
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            UpdatesConnector.Snlp = null;

            base.OnNavigatingFrom(e);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (!string.IsNullOrEmpty(AppSettings.Instance.Password) && !string.IsNullOrEmpty(AppSettings.Instance.Username))
            {
                NavigationService.Navigate(new Uri("/Views/AfterLoginPivotPage.xaml", UriKind.Relative));
                isInit = false;
            }
            else
            {
                UpdatesConnector.Snlp = this;
            }

            base.OnNavigatedTo(e);
        }

        private void ___No_Name__Loaded(object sender, RoutedEventArgs e)
        {
            if (isInit)
            {
                if (NavigationService.CanGoBack)
                    NavigationService.RemoveBackEntry();

                UpdatesConnector.GetBliposphere();
                PicturesConnector.GetNewestPictures();

                ApplicationBar = new ApplicationBar();

                ApplicationBar.IsVisible = true;
                ApplicationBar.Mode = ApplicationBarMode.Minimized;
                ApplicationBar.Opacity = 0.8;
                save = new ApplicationBarIconButton(new Uri("/Images/appbar_okbutton.png", UriKind.Relative));
                save.Click += new EventHandler(save_Click);
                save.Text = "zaloguj";

                ApplicationBarMenuItem refresh = new ApplicationBarMenuItem("odśwież");
                refresh.Click += new EventHandler(refresh_Click);
                ApplicationBar.MenuItems.Add(refresh);

                Login.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(Login_Tap);

                isInit = false;
            }
        }

        void Login_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //todo: teraz po tapnieciu na login zakrywane jest haslo klawiatura
            //Password.Focus();
            //Login.Focus();
        }

        void refresh_Click(object sender, EventArgs e)
        {
            UpdatesConnector.GetBliposphere();
            PicturesConnector.GetNewestPictures();
        }

        void save_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Password.Password) || string.IsNullOrEmpty(Login.Text))
            {
                MessageBox.Show("Podaj dane logowania!");
            }
            else
            {
                AppSettings.Instance.Username = Login.Text;
                AppSettings.Instance.Password = Password.Password;
                NavigationService.Navigate(new Uri("/Views/AfterLoginPivotPage.xaml", UriKind.Relative));
            }
        }

        public void UpdateBliposphereDataSource(List<WebServiceDataSources.Status> statuses)
        {
            Dispatcher.BeginInvoke(() =>
                {
                    BliposphereListBox.DataContext = statuses;
                });
        }

        public void UpdateNewestPhotos(List<WebServiceDataSources.Picture> pictures)
        {
            Dispatcher.BeginInvoke(() =>
            {
                PicturesListBox.DataContext = pictures;
            });
        }

        private void MainPano_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((PanoramaItem)MainPano.SelectedItem).Name == "LoginPano")
            {
                ApplicationBar.Buttons.Add(save);
                ApplicationBar.Mode = ApplicationBarMode.Default;
            }
            else
            {
                ApplicationBar.Buttons.Remove(save);
                ApplicationBar.Mode = ApplicationBarMode.Minimized;
            }
        }

        private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string updatePath = ((Image)sender).Tag.ToString();
            string statusId = updatePath.Split('/').Last();
            NavigationService.Navigate(new Uri("/Views/SingleStatusPage.xaml?SID=" + statusId, UriKind.Relative));
        }

        private void BliposphereListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (BliposphereListBox.SelectedItem != null)
            {
                string statusId = ((WebServiceDataSources.Status)BliposphereListBox.SelectedItem).id.ToString();
                NavigationService.Navigate(new Uri("/Views/SingleStatusPage.xaml?SID=" + statusId, UriKind.Relative));
                BliposphereListBox.SelectedItem = null;
            }
        }
    }
}
